﻿using System;
using System.Collections.Generic;

namespace Mersiv.Lib.Wrapper.Command
{

  /// <summary>
  /// 
  /// "hg status" OUTPUT: "R FileName" where "R" is some output code.
  /// 
  /// 
  /// </summary>
  public class HgStatusCommand : HgCommand
  {

    public class ResultCode
    {
      public string Code { get; set; }
      public string Name { get; set; }
      public ResultCode()
        : this("", "")
      {
      }
      public ResultCode(string code, string name)
      {
        this.Code = code;
        this.Name = name;
      }
    }



    /// <summary>
    /// Input parameters: The names of the files to execute the "status"
    /// command against. If names are provided then the "status" command
    /// only shows output for matching files, otherwise the output matches
    /// all files.
    /// 
    /// NOTE: Not doing anything with this yet.
    /// </summary>
    public List<string> InputFileList { get; set; }


    /// <summary>
    /// The output result from executing the command.
    /// </summary>
    public List<ResultCode> ResultCodeList { get; set; }


    public HgStatusCommand()
    {
      this.Command = "status";
      this.ResultCodeList = new List<ResultCode>();
    }


    public override void ParseResults()
    {
      try
      {
        foreach (string result in base.OutputResultList)
        {
          if (!string.IsNullOrEmpty(result))
          {
            //base.OnCommandOutputParsingCompleted("HgStatusCommand.ParseResults: " + result);
            string[] output = result.Split(new char[] { ' ' });
            if (output.Length > 1)
            {
              ResultCode resultCode = new ResultCode(output[0], output[1]);
              this.ResultCodeList.Add(resultCode);
            }
          }
        }
      }
      catch
      {
        //
        // TODO: Just eating the exception for now...
        //
        base.Error = "ERROR: Command not executed";
      }
      base.OnCommandExecutionCompleted(">>> HG status command completed: " + this.ResultCodeList.Count + " modifications found.");
    }

  }

}